<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/16
 * Time: 13:31
 */

namespace app\admin\controller;


use app\admin\model\AdminUser;
use think\facade\Session;
use think\Controller;

class Login extends Controller
{

    public function login(){
        return $this->fetch();
    }

    public function doLogin(){
        $username = trim(input('post.username'));
        $password = trim(input('post.password'));
        $admin = AdminUser::with('role')->where('username',$username)->find();
        if (!$admin){
            return json(['code'=>0,'msg'=>'用户名不存在']);
        }
        if (!$admin['status']){
            return json(['code'=>0,'msg'=>'用户已禁用']);
        }
        if (!$admin['role']['status']){
            return json(['code'=>0,'msg'=>'角色已禁用']);
        }
        if (md5(md5($username.$password).'ADMIN') != $admin['password']){
            return json(['code'=>0,'msg'=>'密码错误']);
        }
        Session::set('admin_id',$admin['id']);
        Session::set('role_id',$admin['role_id']);
        return json(['code'=>1,'msg'=>'登录成功']);
    }

    public function loginOut(){
        Session::delete('admin_id');
        Session::delete('role_id');
        return json(['code'=>1,'msg'=>'退出成功']);
    }
}